﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using CodeStash.Common.DataAccess.Repository;
using CodeStash.Common.DataAccess.EntityFramework;

namespace CodeStash.IntegrationTests
{
    [TestFixture]
    public class GroupingTests
    {
        [Test]
        public void SimpleInsertAndDeleteTest()
        {
            Repository<Grouping> groupingRepository = new Repository<Grouping>();
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                groupingRepository.EnrolInUnitOfWork(db);
                Guid uniqueId = Guid.NewGuid();
                groupingRepository.Add(new Grouping(uniqueId.ToString()));
                db.SaveChanges();

                IEnumerable<Grouping> groups = groupingRepository.FindBy(x => x.Description == uniqueId.ToString());
                Assert.AreEqual(1, groups.Count());

                groupingRepository.Remove(groups.First());
                db.SaveChanges();

                groups = groupingRepository.FindBy(x => x.Description == uniqueId.ToString());
                Assert.AreEqual(0, groups.Count());
            }
        }

        [Test]
        public void SimpleUpdateTest()
        {
            Repository<Grouping> groupingRepository = new Repository<Grouping>();
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                groupingRepository.EnrolInUnitOfWork(db);
                Guid uniqueId = Guid.NewGuid();
                groupingRepository.Add(new Grouping(uniqueId.ToString()));
                db.SaveChanges();

                IEnumerable<Grouping> groups = groupingRepository.FindBy(x => x.Description == uniqueId.ToString());
                Assert.AreEqual(1, groups.Count());

                Guid uniqueId2 = Guid.NewGuid();
                Grouping addedGroup = groups.First();
                addedGroup.Description = uniqueId2.ToString();
                db.SaveChanges();

                int id = addedGroup.GroupId;

                groups = groupingRepository.FindBy(x => x.GroupId == id);
                Grouping updatedGroup = groups.First();
                Assert.AreEqual(1, groups.Count());
                Assert.AreEqual(uniqueId2.ToString(), updatedGroup.Description);


                groupingRepository.Remove(groups.First());
                db.SaveChanges();

                groups = groupingRepository.FindBy(x => x.GroupId == id);
                Assert.AreEqual(0, groups.Count());
            }
        }
    }
}
